#pragma once

#include "noncopyable.h"
#include "Socket.h"
#include "Channel.h"

#include <functional>

class EventLoop;
class InetAddress;

class Acceptor : noncopyable
{
public:
    using NewConnectionCallback = std::function<void(int sockfd, const InetAddress&)>;

    Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport);
    ~Acceptor();

    void setNewConnectionCallback(const NewConnectionCallback &cb)
    {
        newConnectionCallback_ = cb;
    }

    bool listenning() const { return listenning_; }
    void listen();

private:
    void handleRead();

    EventLoop* loop_; //acceptor用的就是用户定义的baseloop.也称作mainloop
    Socket acceptSocket_;                // 监听 socket
    Channel acceptChannel_;              // 监听 socket 对应的 Channel
    NewConnectionCallback newConnectionCallback_; // 新连接回调
    bool listenning_;                    // 是否正在监听
};